//
// Created by 崔世强 on 2018/1/15.
// Copyright (c) 2018 Facebook. All rights reserved.
//

import Foundation
import WCDBSwift

enum GroupType:Int {
    case official = 1   //官方群
    case normal = 2     //普通群
    case service_platform = 3 //服务平台群
}

extension GroupType: ColumnCodable,LiteralValueConvertible {
    init?(with value: FundamentalValue) {
        self = GroupType(rawValue: Int(value.int32Value)) ?? GroupType.normal
        
    }
    
    func archivedValue() -> FundamentalValue {
        return FundamentalValue(self.rawValue)
    }
    
    static var columnType: ColumnType {
        return .integer64
    }
//
//    typealias FundamentalType = Int
//
//    init?(with value: Int) {
//        self = GroupType(rawValue: value) ?? GroupType.normal
//    }
//
//    func archivedValue() -> Int? {
//        return self.rawValue
//    }
}

final class Group: TableCodable {
    static let table = "groups"

    var id:Int = 0                    //
    var groupId:Int = 0               //群ID
    var groupType:GroupType = .normal   //群类型
    var name:String? = nil              //群名称
    var avatar:String? = nil            //群头像

    var owner:Int = 0                 //群所有人
    var mute:Bool = false               //全体禁言
    var kickout:Bool = false            //本人被踢出标志，和主动退出不同，不会删除通话记录，不可以发送消息
    var top:Bool = false                //置顶标志
    var noDisturb:Bool = false          //免打扰标志

    var announcement:String? = nil      //群公告
    var announceUser:Int = 0          //最后发起公告人
    var announceTime:Date = Date()      //最近发起公告时间

    var createTime:Date = Date()        //创建时间
    var deleted:Bool = false             //删除标志
  
    var servicePlatformID: Int = 0     //服务平台群ID

    enum CodingKeys: String, CodingTableKey {
        typealias Root = Group
//        static let objectRelationalMapping = TableBinding(CodingKeys.self)
        case id
        case groupId = "group_id"
        case groupType = "group_type"
        case name
        case avatar

        case owner
        case mute
        case kickout
        case top
        case noDisturb = "no_disturb"

        case announcement
        case announceUser = "announce_user"
        case announceTime = "announce_time"

        case createTime = "create_time"
        case deleted
        case servicePlatformID
        
        static let objectRelationalMapping = TableBinding(CodingKeys.self) {
            BindColumnConstraint(.id, isPrimary: true, isAutoIncrement: true)
            BindColumnConstraint(.mute, defaultTo: false)
            BindColumnConstraint(.kickout, defaultTo: false)
            BindColumnConstraint(.top, defaultTo: false)
            BindColumnConstraint(.noDisturb, defaultTo: false)
        }

//        static var columnConstraintBindings: [CodingKeys: ColumnConstraintBinding]? {
//            return [.id: ColumnConstraintBinding(isPrimary: true, isAutoIncrement: true),
//                    .mute:ColumnConstraintBinding(defaultTo: false),
//                    .kickout:ColumnConstraintBinding(defaultTo: false),
//                    .top:ColumnConstraintBinding(defaultTo: false),
//                    .noDisturb:ColumnConstraintBinding(defaultTo: false),
//            ]
//        }
    }
    var isAutoIncrement: Bool = true
    var lastInsertedRowID: Int = 0
}
